﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VietKinsNutrition.Bussiness;
using System.Globalization;
using VietKinsNutrition.Common;

namespace VietKinsNutrition.UserControl
{
    public partial class Frm_Hoadondicho : DevExpress.XtraEditors.XtraUserControl
    {
        public Frm_Hoadondicho()
        {
            InitializeComponent();
            datNgay.EditValue = DateTime.Now;
        }
        ClsTblDm_HoadontrongngayBLL ClsTblDm_Hoadontrongngaybll = new ClsTblDm_HoadontrongngayBLL();
        ClsTblDm_XuatkhoBLL ClsTblDm_Xuatkho_bll = new ClsTblDm_XuatkhoBLL();

        private void Frm_Hoadondicho_Load(object sender, EventArgs e)
        {

            txtPhatsinh.Text = "0";
            datNgay_EditValueChanged(sender, e);
        }

        private void datNgay_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                ClsTblDm_Hoadontrongngaybll.Bind_DGrid(grdDanhsach, datNgay.DateTime);
                double tongnhap = 0;
                double tongxuat = 0;
                for (int i = 0; i < grdDanhsach.RowCount; i++)
                {
                    try
                    {
                        tongnhap += Convert.ToDouble(grdDanhsach.GetRowCellValue(i, "SLNhap")) * Convert.ToDouble(grdDanhsach.GetRowCellValue(i, "GiaNhap"));
                    }
                    catch { }
                    try
                    {
                        tongxuat += Convert.ToDouble(grdDanhsach.GetRowCellValue(i, "SLXuat")) * Convert.ToDouble(grdDanhsach.GetRowCellValue(i, "GiaXuat"));
                    }
                    catch { }
                }
                //txtTongnhap.Text = tongnhap.ToString("#,##0");
                //txtTrongngay.Text = tongxuat.ToString("#,##0");
                DataTable Tbl = this.ClsTblDm_Xuatkho_bll.TblXuatchokhoilop_Ngay(datNgay.DateTime);
                try
                {
                    txtPhatsinh.Text = double.Parse(Tbl.Rows[0]["Dongiathucte"].ToString()).ToString("#,##0");
                }
                catch
                {

                    txtPhatsinh.Text = "0";
                }
                txtTongtien.Text = double.Parse(txtTongtien.Text.Trim()).ToString("#,##0");
                grdDanhsach.RefreshData();
            }
            catch { }
        }

        private void CheckKey(KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtPhatsinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            CheckKey(e);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                string a = txtPhatsinh.Text.Replace(",", "");
                ClsTblDm_Xuatkho_bll.UpdatePhatsinhByNgay(a, datNgay.DateTime);
                XtraMessageBox.Show("Cập nhật phát sinh thành công", "Thông báo");
            }
            catch
            {
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            ClsTblDm_HoadondichoBLL ClsTblDm_Hoadondicho_bll = new ClsTblDm_HoadondichoBLL();
            ClsTblGiaBLL ClsTblGiabll = new ClsTblGiaBLL();
            int iGiatien = 0; int iTongsotre_nt = 0; int iTongsotre_mg = 0; string chi_nt = ""; string chi_mg = "";
            int iGiatien2 = 0;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
            nfi.NumberGroupSeparator = ".";
            nfi.NumberDecimalSeparator = ",";
            if (ClsTblGiabll.ReturnTblSelectAll().Rows.Count > 0)
            {
                iGiatien = int.Parse(ClsTblGiabll.ReturnTblSelectAll().Rows[0]["nhatre"].ToString());
                iGiatien2 = int.Parse(ClsTblGiabll.ReturnTblSelectAll().Rows[0]["maugiao"].ToString());
            }
            ClsTblDm_SosanhnangluonBLL ClsTblDm_Sosanhnangluon_bll = new ClsTblDm_SosanhnangluonBLL();

            DataTable TongsotreTbl = new DataTable();
            // trả về tổng sô trẻ.
            try
            {
                TongsotreTbl = ClsTblDm_Sosanhnangluon_bll.GetTongsotre(1, ParamCls.NamhocId, datNgay.DateTime);
            }
            catch { return; }
            if (TongsotreTbl.Rows.Count > 0)
            {
                iTongsotre_nt = int.Parse(TongsotreTbl.Rows[0]["Tongsosuat"].ToString());
            }
            try
            {
                TongsotreTbl = ClsTblDm_Sosanhnangluon_bll.GetTongsotre(2, ParamCls.NamhocId, datNgay.DateTime);
            }
            catch { return; }
            if (TongsotreTbl.Rows.Count > 0)
            {
                iTongsotre_mg = int.Parse(TongsotreTbl.Rows[0]["Tongsosuat"].ToString());
            }
            chi_nt = (iTongsotre_nt * iGiatien).ToString();
            chi_mg = (iTongsotre_mg * iGiatien2).ToString();
            DgHoadondicho.DataSource = gridControl1.DataSource;
            if (this.DgHoadondicho.Rows.Count > 0)
            {
                ClsTblDm_Hoadondicho_bll.ChiPhiKhac = float.Parse(txtPhatsinh.Text);
                ClsTblDm_Hoadondicho_bll.TongTien = float.Parse(txtTongtien.Text);
                ClsTblDm_Hoadondicho_bll.SoTienChi = float.Parse(grdDanhsach.Columns["Giaxuat"].SummaryItem.SummaryValue.ToString());
                ClsTblDm_Hoadondicho_bll.ExportFileExel(this.DgHoadondicho, this.saveFileDialog1, this.datNgay.DateTime, chi_nt, chi_mg, iTongsotre_nt, iTongsotre_mg);
            }
        }

        private void txtPhatsinh_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                txtTongtien.Text = (float.Parse(grdDanhsach.Columns["Giaxuat"].SummaryItem.SummaryValue.ToString()) + float.Parse(txtPhatsinh.Text.Trim())).ToString();
                txtTongtien.Text = double.Parse(txtTongtien.Text.Trim()).ToString("#,##0");
            }
            catch { }
        }

    }
}
